package com.lottery.controller.qiuqiu.wealthController;

import com.alibaba.fastjson.JSONObject;
import com.lottery.controller.BaseController;
import com.lottery.dto.ResultDto;
import com.lottery.service.qiuqiu.wealthService.WealthService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("/api/wealth/")
public class WealthController extends BaseController {
    @Resource
    private WealthService wealthService;

    /**
     * 根据用户Id查询所有的上分记录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/SearchRechargeByUserId", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto SearchRechargeByUserId(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer pageSize = obj.getInteger("pageSize");
            Integer pageNow = obj.getInteger("pageNow");
            Integer state = obj.getInteger("state");
            wealthService.SearchRechargeByUserId(userId, pageNow, pageSize, state, result);
        } catch (Exception e) {
            logger.error("WealthController :: SearchRechargeByUserId error,{}", e);
            result.setResultData(false, "查询数据失败", 1, null);
        }
        return result;
    }

    /**
     * 根据Id查询用户的上分详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/SearchRechargeById", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto SearchRechargeById(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer id = obj.getInteger("id");
            Integer userId = obj.getInteger("userId");
            wealthService.SearchRechargeById(id, userId, result);
        } catch (Exception e) {
            logger.error("WealthController :: SearchRechargeByUserId error,{}", e);
            result.setResultData(false, "查询失败", 1, null);
        }
        return result;
    }

    /**
     * 根据userId查询用户的下分记录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchWithdraw", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchWithdraw(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer pageSize = obj.getInteger("pageSize");
            Integer pageNow = obj.getInteger("pageNow");
            Integer state = obj.getInteger("state");
            wealthService.searchWithdraw(userId, pageNow, pageSize, state, result);
        } catch (Exception e) {
            logger.error("WealthController searchWithdraw error ,{}", e);
            result.setResultData(false, "查询下分信息失败", 1, null);
        }
        return result;
    }

    /**
     * 根据记录id查新下分详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchWithdrawById", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchWithdrawById(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer id = obj.getInteger("id");
            Integer userId = obj.getInteger("userId");
            wealthService.searchWithdrawById(id, userId, result);
        } catch (IOException e) {
            logger.error("WealthController searchWithdrawById error,{}", e);
            result.setResultData(false, "查询失败", 1, null);
        }
        return result;
    }


    /**
     * 取消下分
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/cancelWithdrawById", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto cancelWithdrawById(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer id = obj.getInteger("id");
            wealthService.cancelWithdrawById(id, result);
        } catch (Exception e) {
            logger.error("WealthController cancelWithdrawById error,{}", e);
            result.setResultData(false, "取消下分失败", 1, null);
        }
        return result;
    }

    /**
     * 查询今日盈亏,账户余额
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchWealthInfo", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchWealthInfo(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            int userId = obj.getInteger("userId");
            int loginType = obj.getInteger("loginType") == null ? 0 : obj.getInteger("loginType");
            Integer lotteyType = obj.getInteger("lotteyType") == null ? 1 : obj.getInteger("lotteyType");
            wealthService.searchWealthInfo(userId, loginType, lotteyType, result);
        } catch (Exception e) {
            logger.error("WealthController cancelWithdrawById error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 用户上分申请
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/rechargeByUserId", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto rechargeByUserId(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer money = obj.getInteger("money");
            wealthService.rechargeByUserId(userId, money, result);
        } catch (Exception e) {
            logger.error("WealthController rechargeByUserId error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 用户下分申请
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/withdrawByuserId", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto withdrawByuserId(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer money = obj.getInteger("money");
            Integer cashType = obj.getInteger("cashType");
            String cashAccount = obj.getString("cashAccount");
            wealthService.withdrawByuserId(userId, money, cashAccount, cashType, result);
        } catch (IOException e) {
            logger.error("WealthController cancelWithdrawById error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 我的钱包
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchMypurse", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchMypurse(HttpServletRequest request){
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            String startTime = obj.getString("startTime");
            String endTime = obj.getString("endTime");
            wealthService.searchMypurse(userId,startTime,endTime,result);
        } catch (Exception e) {
            logger.error("WealthController searchMypurse error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

}
